﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Narwhunderful.Aphelia.Framework.Graphics
{

    public class DrawableComponentCollection<T> : UpdatableComponentCollection<T> where T : IDrawableComponent
    {

        IComparer<T> _drawableComponentComparer = new DrawableComponentComparer<T>();
        protected virtual IComparer<T> DrawableComponentComparer
        {
            get
            {
                return _drawableComponentComparer;
            }
            set
            {
                _drawableComponentComparer = value;
            }
        }

        List<T> _drawableComponents = new List<T>();
        public virtual List<T> DrawableComponents
        {
            get
            {
                return _drawableComponents;
            }
        }

        public DrawableComponentCollection()
            : base()
        {
        }

        #region IComponentColection

        protected override void OnComponentAdded(ComponentEventArgs<T> e)
        {
            _drawableComponents.Add(e.Component);
            base.OnComponentAdded(e);
        }

        protected override void OnComponentRemoved(ComponentEventArgs<T> e)
        {
            _drawableComponents.Remove(e.Component);
            base.OnComponentRemoved(e);
        }

        protected override void OnSorting()
        {
            if (_drawableComponentComparer != null) _drawableComponents.Sort(_drawableComponentComparer);
            base.OnSorting();
        }

        #endregion

        #region IDisposeable

        protected override void Dispose(bool disposing)
        {
            if (disposing) _drawableComponents.Clear();
            base.Dispose(disposing);
        }

        #endregion

        public virtual void Draw(GameTime gameTime)
        {
            for (int x = 0; x < _drawableComponents.Count - 1; x++) _drawableComponents[x].Draw(gameTime); 
        }

    }

}
